class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        vector<int> tmp;
        sort(nums.begin(), nums.end());
        int prev = 0;
        int next = 1;
        int sz = nums.size();
        while (next < sz)
        {
            if (nums[prev] != nums[next])
            {
                if (next + 1 == sz)
                {
                    if (tmp.size() == 0)
                    {
                        tmp.push_back(nums[prev]);
                    }
                    tmp.push_back(nums[next]);
                    break;
                }
                if (nums[next] == nums[next + 1])
                {
                    tmp.push_back(nums[prev]);
                    prev += 3;
                    next += 3;
                }
                else
                {
                    tmp.push_back(nums[prev]);
                    tmp.push_back(nums[next]);
                    break;
                }
            }
            else
            {
                prev += 2;
                next += 2;
            }
        }
        if (tmp.size() != 2)
        {
            tmp.push_back(nums.back());
        }
        return tmp;
    }
};